package com.starrin.tennisfinder.extractors;

import com.starrin.tennisfinder.model.BookingStatus;
import com.starrin.tennisfinder.model.HourAvailability;
import com.starrin.tennisfinder.model.VenueAvailabilityDataHolder;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by larsstarrin on 07/02/14.
 */
public class ExtractorSalk {

    public static final String PATTERN = "Ledig<br>Bana [0-9]{1,2}<br> (?<hour>[0-9]{2})";

    public VenueAvailabilityDataHolder extractVenueAvailability(String response, String url) {

        VenueAvailabilityDataHolder dataHolder = new VenueAvailabilityDataHolder();

        Set<Integer> availableHours = new LinkedHashSet<>();
        Pattern pattern = Pattern.compile(PATTERN);
        Matcher matcher = pattern.matcher(response);
        while (matcher.find()) {
            availableHours.add(Integer.valueOf(matcher.group("hour")));
        }
        for(int i=7; i<23; i++) {
            if(availableHours.contains(i)) {
                dataHolder.addAvailability(new HourAvailability(i, BookingStatus.INDOORS, url));
            } else {
                dataHolder.addAvailability(new HourAvailability(i, BookingStatus.BOOKED, null));
            }
        }
        return dataHolder;
    }
}
